package com.example.administrator.text;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
import app.BaseActivity;
import app.MyApplication;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import data.SharedPreferencesUtils;
import util.GreenTreeNetworkUtil;
import util.HomeUtil;
import util.JumpActivityUtils;
import util.LogUtil;
import util.NetUtils;
import util.RequestDataCallback;
import util.StringUtil;
import util.Url;
import view.diaLogView.CrowdRetunDialog;
import view.diaLogView.UploadDialog;

/**
 * 我的时长
 */
public class WeTimeActivity extends BaseActivity {
    private String mId,mToken;
    private String mWevVip = "";
    private UploadDialog mUploadDialog;
    @Bind(R.id.activity_wetime_ll)         RelativeLayout mRelativeLayout;
    @Bind(R.id.activity_wetime_time)       TextView mTextViewTime;     //时长
    @Bind(R.id.activity_wetime_branch)     TextView mTextViewBranch;  //分钟
    @Bind(R.id.activity_wetime_money)      TextView mTextViewMoney; //会员押金
    @Bind(R.id.activity_wetime_networking)TextView mTVNetworking;
    @Bind(R.id.activity_wetime_ll1)        LinearLayout mLinearLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wetime);
        ButterKnife.bind(this);
        HomeUtil.getHemeUtilNew().getStatusBar(this, mRelativeLayout);
        init();

    }

    private void init() {

        mId = (String) SharedPreferencesUtils.getParam(this, HomeUtil.mUseridDB,"");
        mToken = (String) SharedPreferencesUtils.getParam(this, HomeUtil.mTokenDB, "");
        if(!(boolean) SharedPreferencesUtils.getParam(WeTimeActivity.this,HomeUtil.mVIPSTATE,true)){
            mLinearLayout.setVisibility(View.GONE);
            mTVNetworking.setText("加入会员");
        }else {
            if(StringUtil.getStringUtilNew().judgeUIType(MyApplication.getApp().getmPattern()) == 1){
                mLinearLayout.setVisibility(View.VISIBLE);
                mTVNetworking.setText("时长充值");
            }
        }
        queryVip();
        queryBalance();

    }

    /**
     * 马上推荐
     */
    @OnClick(R.id.activity_wetime_recommend)
    void recommend(){
        JumpActivityUtils.getInstance().jumpShare(this);
    }

    /**
     * 充值
     */
    @OnClick(R.id.activity_wetime_networking)
    void networking(){
        if(mTVNetworking.getText().toString().equals("时长充值")){
            JumpActivityUtils.getInstance().jumpTimeRecharge(WeTimeActivity.this, "");
        }else {
            JumpActivityUtils.getInstance().jumpWebView(this,Url.mATIAIRIP + Url.getUrlNew().vipdePosit+"?userid="+mId+"&vipid="+mWevVip+"&pattern="+MyApplication.getApp().getmPattern()+"&appType=1","会员购买");
        }
    }

    /**
     * 解绑会员
     */
    @OnClick(R.id.activity_wetime_relieve)
    void relieve(){
        if((boolean) SharedPreferencesUtils.getParam(WeTimeActivity.this,HomeUtil.mVIPSTATE,true)) {
            String url = Url.mATIAIRIP +Url.getUrlNew().vipmySelf+"?userid="+mId+"&vipid="+mWevVip+"&pattern="+MyApplication.getApp().getmPattern()+"&appType=1";
            JumpActivityUtils.getInstance().jumpWebView(WeTimeActivity.this,url,"会员");
        }else {
            JumpActivityUtils.getInstance().jumpWebView(this,Url.mATIAIRIP + Url.getUrlNew().vipdePosit+"?userid="+mId+"&vipid="+mWevVip+"&pattern="+MyApplication.getApp().getmPattern()+"&appType=1","会员购买");
        }
    }

    @Override
    protected void onResume() {
        super.onPause();
        HomeUtil.getHemeUtilNew().getHandlerNew().postDelayed(new Runnable() {
            @Override
            public void run() {
                init();
            }
        },1000);

    }

    /**
     * 查询余额
     */
    private void queryBalance(){
        String url = "";
        HashMap mMap = new HashMap<>();
        mMap.put("userid",mId);
        String stringJson = StringUtil.getStringUtilNew().hashMapToJson(mMap); //参数包
        long time = StringUtil.getStringUtilNew().getTimeCurrent();
        String stirngSign = StringUtil.getStringUtilNew().getSign(Url.mStirngpcDeal_selectBalance,stringJson,mToken,mId,time);
        Map map = StringUtil.getStringUtilNew().getMapNew(this,stringJson,mToken,mId, Url.mStirngpcDeal_selectBalance,stirngSign,time);
        if(NetUtils.getInstance(this).getNetWorkTeyt()  == 0){
            url = Url.getUrlNew().getAppIP();
        }else {
            url = MyApplication.getApp().getFWQString();
        }
        LogUtil.eE("fwq","  "+url);
        GreenTreeNetworkUtil.getInstance().doPost(this,url,1,map,new RequestDataCallback() {
            @Override public void onSuccess(String response) {
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    String code = jsonObject.getString("code");
                    LogUtil.eE("",response);
                    if(code.equals("1")) {
                        int timeInt = (jsonObject.getInt("longTime"));
                        int minute = timeInt%60;
                        int time = timeInt/60;
                        mTextViewTime.setText(time+"");
                        mTextViewBranch.setText(minute+"");
                    }else if (code.equals("-2")) {
                        /*CrowdRetunDialog crowdRetunDialog = new CrowdRetunDialog(getActivity());
                        crowdRetunDialog.show();*/
                    }else {
                        mTextViewTime.setText("- -");
                        mTextViewBranch.setText("- -");
                    }

                } catch (Exception e) {e.printStackTrace();}
            }

            @Override public void onFailure(Throwable error) {
                mTextViewTime.setText("- -");
                mTextViewBranch.setText("- -");
            }
        });
    }

    /**
     * 明细
     */
    @OnClick(R.id.activity_wetime_detailed)
    void detailed(){
           JumpActivityUtils.getInstance().jumpApiCloud1(this,mId,mToken);
    }

    /**
     * 放回
     */
    @OnClick(R.id.activity_wetime_back)
    void back(){
        finish();
    }

    /**
     * 查询VIP
     */
    private void queryVip(){
        String url = "";
        mMap = new HashMap<>();
        mMap.put("userid",mId);
        String stringJson = StringUtil.getStringUtilNew().hashMapToJson(mMap); //参数包
        long time = StringUtil.getStringUtilNew().getTimeCurrent();
        String stirngSign = StringUtil.getStringUtilNew().getSign(Url.mStirngpcDeal_selectVipmember,stringJson,mToken,mId,time);
        Map map = StringUtil.getStringUtilNew().getMapNew(this,stringJson,mToken,mId, Url.mStirngpcDeal_selectVipmember,stirngSign,time);
        if(NetUtils.getInstance(this).getNetWorkTeyt() == 0){
            url = Url.getUrlNew().getAppIP();
        }else {
            url = MyApplication.getApp().getFWQString();
        }
        getLogger().info("queryVip()--->查询VIP--fwq"+url+"stirngSign"+stirngSign);
        GreenTreeNetworkUtil.getInstance().doPost(this,url,1,map,new RequestDataCallback() {
            @Override public void onSuccess(String response) {
                try {
                    getLogger().info("queryVip()--->onSuccess--String"+response);
                    JSONObject jsonObject = new JSONObject(response);
                    String code = jsonObject.getString("code");
                    LogUtil.eE("",response);

                    if (code.equals("1")) {
                        int pledge = jsonObject.getInt("pledge");
                        mTextViewMoney.setText(pledge+"元");
                        mWevVip = jsonObject.getInt("vipnum")+"";
                    } else if (code.equals("-2")) {
                        CrowdRetunDialog crowdRetunDialog = new CrowdRetunDialog(WeTimeActivity.this);
                        crowdRetunDialog.show();
                    }
                } catch (Exception e) {e.printStackTrace();}
            }

            @Override public void onFailure(Throwable error) {

            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
